'use strict';

module.exports = (app) => {
  class CaseService extends app.Service {
    async create() {
      const { app, ctx } = this;
      const { Case, Character, CaseClassify } = app.model.Tables.Web;
      const data = ctx.request.body;
      let isCharacter = await Character.findByPk(data.characterId);
      if (!isCharacter) {
        return 'characterId不存在';
      }

      let res = await Case.create({
        cover: data.cover,
        name: data.name,
        sketch: data.sketch,
        content: data.content,
        characterId: data.characterId,
        projectId: ctx.adminInfo.project_id,
      });
      if (res) {
        for (let i = 0; i < data.classifyArr.length; i++) {
          await CaseClassify.create({
            caseId: res.dataValues.id,
            classifyId: data.classifyArr[i],
            projectId: ctx.adminInfo.project_id,
          });
        }
      }
      console.log(res);
      return res;
    }

    async delete() {
      const { app, ctx } = this;
      const { Case } = app.model.Tables.Web;
      const { id } = ctx.params;
      let res = await Case.destroy({
        where: {
          id,
          projectId: ctx.adminInfo.project_id,
        },
      });
      if (res) {
        return '删除成功';
      } else {
        return 'id不存在,删除失败';
      }
    }

    async update() {
      const { app, ctx } = this;
      const { Case, CaseClassify, Character } = app.model.Tables.Web;
      const data = ctx.request.body;
      let isCharacter = await Character.findByPk(data.characterId);
      if (!isCharacter) {
        return 'characterId不存在';
      }
      let res = await Case.update(
        {
          cover: data.cover,
          name: data.name,
          sketch: data.sketch,
          content: data.content,
          characterId: data.characterId,
          projectId: ctx.adminInfo.project_id,
        },
        {
          where: {
            id: data.id,
            projectId: ctx.adminInfo.project_id,
          },
        }
      );
      if (res) {
        await CaseClassify.destroy({
          where: {
            caseId: data.id,
          },
        });
        for (let i = 0; i < data.classifyArr.length; i++) {
          await CaseClassify.create({
            caseId: data.id,
            classifyId: data.classifyArr[i],
            projectId: ctx.adminInfo.project_id,
          });
        }
      }
      if (res[0]) {
        return '修改成功';
      } else {
        return 'id不存在,修改失败';
      }
    }

    async getAll() {
      const { app, ctx } = this;
      const { Case, Classify } = app.model.Tables.Web;
      const data = ctx.query;
      let res = await Case.findAndCountAll({
        include: [{ model: Classify }],
        where: {
          projectId: data.projectId || ctx.adminInfo.project_id,
        },
        limit: Number(data.limit) || 10,
        offset: Number(data.offset) || 0,
      });

      return res;
    }

    async getById() {
      const { app, ctx } = this;
      const { Case, Classify } = app.model.Tables.Web;
      const data = ctx.query;
      let res = await Case.findOne({
        include: [{ model: Classify }],
        where: {
          id: data.id,
          projectId: data.projectId || ctx.adminInfo.project_id,
        },
      });
      return res;
    }
  }

  return CaseService;
};
